package com.sita.bluetactivity.TripFragment;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.sita.bluetactivity.Base.BaseActivity;
import com.sita.bluetactivity.Base.BaseMapFragment;
import com.sita.bluetactivity.Base.DownLoaderTask;
import com.sita.bluetactivity.Base.FileUtils;
import com.sita.bluetactivity.Base.ZipExtractorTask;
import com.sita.bluetactivity.NewBlueTooth.BtService;
import com.sita.bluetactivity.R;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import de.greenrobot.event.EventBus;
/**
 * Created by Jason on 2016/10/9.
 */

public class TripMapActivity extends BaseActivity implements View.OnClickListener{
    private BaseMapFragment baseMapFragment;
    private static String fileUri;
    private static String track;
    private ProgressDialog dialog;
    private TripPointBean bean;
    private TextView allTrip,speed,soc;
    private static int socT,speedT;
    private static double distanceD;
    private LinearLayout back;
    private TextView head;
    private ImageView headImg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    public static void jumpTripActivity(Context context, String tripUri, long id
    ,int speed,int soc,double distance){
        Intent intent = new Intent(context,TripMapActivity.class);
        fileUri = tripUri;
        track = String.valueOf(id);
        socT = soc;
        speedT = speed;
        distanceD = distance;
        context.startActivity(intent);
    }
    @Override
    protected void initData() {

        allTrip.setText(String.valueOf(distanceD).substring(0,2)+"km");
        speed.setText("21.3"+"km/h");
        soc.setText("45%");
        head.setText("行程详情");
        beginsetData();
    }

    @Override
    protected void initView() {
        allTrip = bindView(R.id.alltrip);
        speed = bindView(R.id.speed);
        soc = bindView(R.id.ave_soc);
        back = bindView(R.id.back_layout);
        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(this);
        head = bindView(R.id.head_tx);
        headImg = bindView(R.id.head_logo);
        headImg.setVisibility(View.GONE);
        android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (baseMapFragment==null){
            baseMapFragment = new BaseMapFragment();
            transaction.add(R.id.trip_map,baseMapFragment);
        }

        else {
            transaction.show(baseMapFragment);
        }
        transaction.commit();
    }
    @Override
    protected int getId() {
        return R.layout.activity_trip_map;
    }
    private void beginsetData(){
        dialog = ProgressDialog.show(this,null,"路线读取中...");
        dialog.show();
        new FileUtils.DownloadFileAsync().execute(fileUri, track);
        dialog.dismiss();

    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void onEventMainThread(String event) {
        bean = new TripPointBean();
        if (doReadFile(event)!=null){
            bean = new Gson().fromJson(doReadFile(event),TripPointBean.class);
        }
        EventBus.getDefault().post(bean.getPoints());
       }

    //解析Json文件
    private String doReadFile(String event) {
        File file = new File(event);
        StringBuilder sb = null;
        try {
            String encoding="UTF-8";
            sb = new StringBuilder();
            if (file.isFile() && file.exists()) { //判断文件是否存在
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file),encoding);//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    sb.append(lineTxt);
                }
                read.close();
            } else {
                // logger.warn("找不到文件" + file.getPath() + File.separator + file.getName());
            }
        } catch (Exception e) {
            // logger.error("分享行程中,读取文件出错,文件:" + file.getPath() + File.separator + file.getName());
        }

        return new String(sb);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back_layout:
                finish();
                break;
        }
    }
}
